/*
 * Depends:
 *	jquery.ui.core.js
 *	jquery.ui.widget.js
 */
(function($)
{
	$.widget("ui.placeholder", {

		_create: function()
		{
			var elm = this.element;
			var tag = elm[0].tagName;
			var inp = document.createElement(tag);
			if ("placeholder" in inp || !elm.attr('placeholder'))
				return;

			var shitOldOpera = $.browser.opera && $.browser.version<11;
			
			this.ph = $(inp);
			var ph = this.ph;
			ph.val(elm.attr('placeholder'));
			ph[0].defaultValue = elm.attr('placeholder');
			ph.attr('readonly', '');
			ph.attr('iAmThePlaceholder',1);
			ph.css({
				position: 'absolute',
				background: 'transparent',
				border: 0,
				color: '#6b6b6b',
				overflow: 'hidden',
				top: shitOldOpera ? 0 : elm.css('top'),
				left: shitOldOpera ? 0 : elm.css('left'),
				marginTop: parseInt(elm.css('marginTop')),
				marginLeft: parseInt(elm.css('marginLeft')),
				marginBottom: parseInt(elm.css('marginBottom')),
				marginRight: parseInt(elm.css('marginRight')),
				paddingTop: parseInt(elm.css('paddingTop')) + parseInt(elm.css('borderTopWidth'))*2,
				paddingLeft: parseInt(elm.css('paddingLeft')) + parseInt(elm.css('borderLeftWidth'))*2,
				paddingBottom: parseInt(elm.css('paddingBottom')) + parseInt(elm.css('borderBottomWidth'))*2,
				paddingRight: parseInt(elm.css('paddingRight')) + parseInt(elm.css('borderRightWidth'))*2,
				width: elm.width() - 2 ,
				height: elm.height() - 2,
				font: elm.css('font'),
				fontSize: elm.css('fontSize'),
				zIndex: (parseInt(elm.css('zIndex')) || 0) + 1
			});

			var wrapper = $("<div style='position:relative;'></div>");
			elm.wrap(wrapper);
			ph.insertBefore(elm);

			ph.bind("focus", function()
			{
				elm.focus();
				ph.hide();
				ph.attr('disabled', '');
			});
			elm.bind("focus", function()
			{
				ph.hide();
				ph.attr('disabled', '');
			});
			elm.bind("blur", function()
			{
				var state = !!String(elm.val()).length;
				if (state)
				{
					ph.attr('disabled', '');
					ph.hide();

				} else
				{
					ph.removeAttr('disabled');
					ph.show();
				}
			});

			elm.blur();
		},

		destroy: function()
		{
			if (this.ph)
			{
				this.ph.remove();
				this.element.unwrap();
			}

			$.Widget.prototype.destroy.apply(this, arguments);
		}
	});

})(jQuery);

$(document).ready(function(){
	$("[placeholder]").livequery(function()
	{
		$(this).placeholder();
	});
});